//获取token的通用请求
import {
	requestURL
} from './config'

import { jwt_token, setToken, getToken, removeToken } from '@/common/utils/auth.js';

function getTokenRequest(data) {
	const input = data.data
	return new Promise((resolve, reject) => {
		uni.request({
			url: requestURL + data.url,
			data: input,  
			method: data.method,
			header: {
				'content-type': 'application/json'
			},
			success: (res) => {
				if (res.statusCode == 200) {
					console.log("成功200")
					// console.log('tokenHead+token 来了');
					console.log(res)
					if(res.data.data ==null){
						uni.showToast({
							title: res.data.message,
							icon: 'none',
							mask: true,
							duration: 1000
						})
						setToken(null);
					}else {
						setToken(res.data.data.tokenHead + ' ' + res.data.data.token);
					}
					console.log(getToken());
					resolve(res)
				} else {
					console.log("失败1")
					reject(res)
				}
			},
			fail: (res => {
				  console.log("失败2")
				  reject(res)
			})
		});
	})
}


export default function(data, url, method) {
	return getTokenRequest(data, url, method)
}
